﻿using System;
using System.Collections.Generic;
using System.Text;
using Echevil;

namespace WindowService
{
    public class G3Helper : ComHelper
    {
        public string NetworkCardName = "";
        public string ApnName = "3GNET";
        public G3Helper(string portName, int baudRate = 9600, int timeOut = 30000)
            : base(portName, baudRate, timeOut)
        {

        }
        private string getError(string result)
        {
            int successPos = result.IndexOf("OK");
            if (successPos == -1) return null;
            int errorPos = result.IndexOf("CME ERROR:");
            if (errorPos == -1 || result.Length <= (errorPos + 10)) return null;
            int startPos = errorPos + 10;
            int stopPos = result.IndexOf("\r\n");
            stopPos = (stopPos == -1 || stopPos <= startPos) ? (result.Length - startPos) : (stopPos - startPos);
            string errorStr = result.Substring(startPos, stopPos);
            errorStr = errorStr.Replace("\r\n", "").Trim();

            Log.WriteLog("G3Helper Get Error Message:" + errorStr, LogType.Error);
            return errorStr;
        }

        //获取信号强度
        public string getWsig()
        {
            string result = SendCommand("AT+CSQ");
            //result = "AT+CSQ\r\n\r\n+CSQ: 31,99\r\n\r\nOK\r\n"

            string error = getError(result);
            if (!string.IsNullOrEmpty(error))
            {
                return error;
            }

            int csqPos = result.IndexOf("+CSQ:") + 5;
            int berPos = result.IndexOf(",99");
            if (csqPos == -1 || berPos == -1 || csqPos > result.Length || berPos > result.Length || csqPos >= berPos)
            {
                return "error";
            }
            int rssi = Convert.ToInt32(result.Substring(csqPos, (berPos - csqPos)).Trim());
            if (rssi < 0 || rssi > 31)
            {
                return "error";
            }
            if (rssi == 0) return "<= -113 dBm";
            if (rssi == 31) return ">= -51 dBm";
            rssi = -113 + rssi * 2;
            return rssi + " dBm";
        }
        //获取网络状态
        public bool isAttached()
        {
            string result = SendCommand("AT+CGATT=1");

            string error = getError(result);
            if (!string.IsNullOrEmpty(error))
            {
                //错误
                return false;
            }
            result = SendCommand("AT+CGATT?");

            error = getError(result);
            if (!string.IsNullOrEmpty(error))
            {
                //错误
                return false;
            }
            if (result.Contains("1")) return true;
            return false;
        }
        //拨号
        public bool connect()
        {
            string result = SendCommand("AT^NDISDUP=1,1,\""+ ApnName+"\"");
            string error = getError(result);
            if (!string.IsNullOrEmpty(error))
            {
                //错误
                return false;
            }

            return true;
        }

        public string getWSpeed()
        {
            NetworkMonitor monitor = new NetworkMonitor();
            foreach (NetworkAdapter adapter in monitor.Adapters)
            {
                if (adapter.Name == this.NetworkCardName)
                {
                    return adapter.DownloadSpeedKbps + " kbps";
                }
            }
            return "";
        }
    }

}
